<template>
  <div class="app-container">
    <div class="filter-container">
        <el-form inline>
            <el-form-item label="班级名称">
                <el-input v-model="searchQuery.name" placeholder="请输入班级名称" />
            </el-form-item>
            <el-form-item label="老师">
                <el-input
                    v-model="searchQuery.teacher_name"
                    placeholder="选择老师"
                    clearable
                    @click.native="filterTeacherVisible = true"
                    @clear="() => { searchQuery.teacher_id = 0; searchQuery.teacher_name = ''; return false }"
                    />
                <select-teacher
                    :visible.sync="filterTeacherVisible"
                    @select="(res) => { searchQuery.teacher_id = res.id, searchQuery.teacher_name = res.name }"
                    />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="danger" icon="el-icon-plus" @click="showClassDialog()">添加</el-button>
            </el-form-item>
        </el-form>
    </div>
    <div class="table-container">
        <el-table :data="pageInfo.data" border highlight-current-row header-cell-class-name="table-header">
            <el-table-column prop="id" label="ID" width="60" />
            <el-table-column prop="name" label="班级名称" />
            <el-table-column prop="start_date" label="开班日期" />
            <el-table-column label="人数控制" width="80">
                <template slot-scope="scope">
                    {{ scope.row.min_num }}/{{ scope.row.max_num }}
                </template>
            </el-table-column>
            <el-table-column prop="total_num" label="实际人数" width="80" />
            <el-table-column prop="course_name" label="课程" width="300">
                <template slot-scope="scope">
                    <div v-for="course in scope.row.courses" :key="course.id">
                        【{{ course.teacher_name }}】{{ course.course_name }}
                        <el-icon type="delete" class="el-icon-edit" @click.native="editCourse(course, scope.row)" />
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                    <el-button type="text" @click="showCourseDialog(scope.row)">加课</el-button>
                    <router-link :to="{name: 'ClassStudent', query: {classId: scope.row.id}}"><el-button type="text">学生</el-button></router-link>
                    <el-button type="text" @click="showClassDialog(scope.$index)">编辑</el-button>
                    <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            :current-page.sync="pageInfo.current_page"
            :page-size.sync="pageInfo.per_page"
            :total="pageInfo.total"
            layout="total, prev, pager, next, jumper"
            @current-change="getList"
        />
    </div>
    <!-- 添加班级弹窗框 -->
    <div class="dialog-container">
        <el-dialog :title="classDialogTitle" :visible.sync="classDialogVisible" :close-on-click-modal="false" with="300px">
            <el-form :model="temp" label-width="100px">
                <el-form-item label="班级名称">
                    <el-input v-model="temp.name" class="w-250" />
                </el-form-item>
                <el-form-item label="开班日期">
                    <el-date-picker v-model="temp.start_date" type="date" value-format="yyyy-MM-dd" />
                </el-form-item>
                <el-form-item label="人数控制" prop="min_num">
                    <div class="felx items-center">
                        <el-input v-model.number="temp.min_num" class="w-100" />
                        <span class="ml-5 mr-5">至</span>
                        <el-input v-model.number="temp.max_num" class="w-100" />
                        <span class="ml-5">人</span>
                    </div>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="classDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleEdit()">确定</el-button>
            </span>
        </el-dialog>
    </div>
    <!-- 添加课程弹窗框 -->
    <div class="dialog-container">
        <el-dialog title="添加课程" :visible.sync="courseDialogVisible" width="30%">
            <el-form :model="tempCourse" label-width="100px">
                <el-form-item label="班级">
                    <el-input v-model="tempCourse.class_name" class="w-250" disabled />
                </el-form-item>
                <el-form-item label="课程名称">
                    <el-input v-model="tempCourse.course_name" class="w-250" readonly :disabled="tempCourse.id > 0" @click.native="selectCourseVisible = true" />
                </el-form-item>
                <el-form-item label="教师">
                    <el-input v-model="tempCourse.teacher_name" class="w-250" readonly @click.native="selectTeacherVisible = true" />
                    <span style="color:red; line-height: 15px; font-size: 12px;display: inline-block;margin-top:10px;">
                        注：如果替换了老师，那么今天和之后的排课会<br />全部替换为本老师
                    </span>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button v-if="tempCourse.id > 0" type="danger" @click="deleteCourse(tempCourse.id)">删除</el-button>
                <el-button @click="courseDialogVisible = false">取消</el-button>
                <el-button type="primary" @click="addCourse()">确定</el-button>
            </span>
            <select-teacher :visible.sync="selectTeacherVisible" @select="(res) => {tempCourse.teacher_id = res.id, tempCourse.teacher_name = res.name}" />
            <select-course :visible.sync="selectCourseVisible" @select="res => {tempCourse.course_id = res.id, tempCourse.course_name = res.name}" />
        </el-dialog>
    </div>
  </div>
</template>
<script>
import courseApi from '@/api/course'
import teacherApi from '@/api/teacher'
import classApi from '@/api/class'

import SelectCourse from '../components/SelectCourse.vue'
import SelectTeacher from '../components/SelectTeacher.vue'
import { resetObject } from '@/utils'

export default {
    name: 'ClassIndex',
    components: { SelectCourse, SelectTeacher },
    data() {
        return {
            courses: [],
            teachers: [],
            classDialogTitle: '',
            classDialogVisible: false,
            courseDialogVisible: false,
            selectCourseVisible: false,
            selectTeacherVisible: false,
            pageInfo: {
                current_page: 1,
                per_page: 10,
                total: 0,
                data: []
            },
            filterTeacherVisible: false,
            searchQuery: {
                name: '',
                teacher_id: 0,
                teacher_name: ''
            },
            temp: {
                id: 0,
                name: '',
                start_date: '',
                min_num: 1,
                max_num: 30
            },
            tempCourse: {
                id: 0,
                class_id: 0,
                class_name: '',
                course_id: 0,
                course_name: '',
                teacher_id: 0,
                teacher_name: ''
            }
        }
    },
    created() {
        this.getList()
        courseApi.list().then(res => { this.courses = res.data }).catch(() => {})
        teacherApi.list().then(res => { this.teachers = res.data }).catch(() => {})
    },
    methods: {
        getList() {
            const params = {
                page: this.pageInfo.current_page,
                per_page: this.pageInfo.per_page,
                name: this.searchQuery.name,
                teacher_id: this.searchQuery.teacher_id
            }
            classApi.page(params).then(res => {
               this.pageInfo = res.data
            })
        },
        search() {
            this.pageInfo.current_page = 1 // 重置为第一页
            this.getList() // 调用获取列表的方法，传递当前页码和每页数量作为参数
        },
        handleEdit() {
            this.classDialogVisible = false
            if (this.temp.id) {
                classApi.edit(this.temp).then(() => {
                    this.getList()
                    this.$message.success('编辑成功')
                })
            } else {
                classApi.add(this.temp).then(() => {
                    this.pageInfo.current_page = 1 // 重置为第一页
                    this.getList()
                    this.$message.success('添加成功')
                })
            }
        },
        handleDelete(row) {
            this.$confirm('此操作将永久删除该班级, 是否继续?', '提示').then(() => {
                classApi.delete(row.id).then(() => {
                    this.getList()
                    this.$message.success('删除成功')
                })
            }).catch(() => {})
        },
        showClassDialog(index) {
            this.classDialogVisible = true
            if (typeof index === 'undefined') {
                this.classDialogTitle = '添加班级'
                if (this.temp.id) {
                   resetObject(this.temp) // 重置对象为初始状态，确保每次打开弹窗时都是空的
                }
            } else {
                this.classDialogTitle = '编辑班级'
                if (this.temp.id !== this.pageInfo.data[index].id) {
                    this.temp = Object.assign({}, this.pageInfo.data[index])
                }
            }
        },
        showCourseDialog(row) {
            resetObject(this.tempCourse) // 重置对象为初始状态，确保每次打开弹窗时都是空的
            this.tempCourse.class_id = row.id
            this.tempCourse.class_name = row.name
            this.courseDialogVisible = true
        },
        addCourse() {
            classApi.addCourse(this.tempCourse).then((res) => {
                if (res.code === 200) {
                    this.courseDialogVisible = false
                    this.getList()
                    this.$message.success('操作成功')
                } else {
                    this.$message.error(res.msg)
                }
            }).catch(() => {})
        },
        // 编辑班级课程
        editCourse(course, classInfo) {
            this.tempCourse = Object.assign({}, course)
            this.tempCourse.class_name = classInfo.name
            this.courseDialogVisible = true
        },
        deleteCourse(id) {
            this.$confirm('确定删除该课程吗？', '提示').then(() => {
                classApi.deleteCourse(id).then(() => {
                    this.getList()
                    this.$message.success('删除成功')
                })
            }).catch(() => {})
        }
    }
}
</script>
